import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Jiang Jinxi
 * Date: 2024-04-13
 * Time: 18:11
 */
public class Test1 {
    public static void print(char[][] arr){
        System.out.println(" -------------");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(" | ");
                System.out.print(arr[i][j]);
            }
            System.out.print(" | ");
            System.out.println();
            System.out.println(" -------------");
        }
    }
    public static char isWin(char[][] arr,int row,int col){
        if(row == col || row == 2 && col == 0 || row == 0 && col == 2 ) {
            if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] ||
                    arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] ) {
                return arr[1][1];
            }
        }
        if(arr[row][0] == arr[row][1] && arr[row][0] == arr[row][2] ||
                arr[0][col] == arr[1][col] && arr[0][col] == arr[2][col]) {
            return arr[row][col];
        }
        return ' ';

    }
    public static boolean isDraw(char[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] == ' ') {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        char[][] arr = new char[3][3];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = ' ';
            }
        }
        print(arr);
        while(true){
            System.out.print("Enter a row(0,1 or 2) for player X:");
            int rowX = scanner.nextInt();
            System.out.print("Enter a column(0,1 or 2) for player X:");
            int colX = scanner.nextInt();
            arr[rowX][colX] = 'X';
            print(arr);
            if(isDraw(arr)){
                System.out.println("draw");
                return;
            }

            if(isWin(arr,rowX,colX) == 'X'){
                System.out.println("X player won");
                break;
            }
            System.out.print("Enter a row(0,1 or 2) for player O:");
            int rowO = scanner.nextInt();
            System.out.print("Enter a column(0,1 or 2) for player O:");
            int colO = scanner.nextInt();
            arr[rowO][colO] = 'O';
            print(arr);
            if(isDraw(arr)){
                System.out.println("draw");
                return;
            }
            if(isWin(arr,rowO,colO) == 'O'){
                System.out.println("O player won");
                break;
            }


        }
    }
}
